package com.cdream.basic.mq.kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * @author cdream
 * @date 2022/8/7
 */
public class KafkaProducerOffsetDemo {
    private static final String BROKER_LIST = "192.168.0.118:9092";
    private static final String TOPIC = "mytopic";

    public static void main(String[] args) throws IOException {
        Properties props = new Properties();
        props.put("key.serializer", StringSerializer.class.getName());
        props.put("value.serializer", StringSerializer.class.getName());
        props.put("bootstrap.servers", BROKER_LIST);

        KafkaProducer<String,String> producer = new KafkaProducer<>(props);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s ;
        while ((s=br.readLine())!=null){
            ProducerRecord<String, String> producerRecord = new ProducerRecord<>(TOPIC,"key:"+s, s);
            try {
                producer.send(producerRecord);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        producer.close();
    }

}
